<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    public function list()
    {
        return $this->response->array(
            Banner::query()
                ->with('attachments')
                ->orderBy('sort_id')
                ->paginate()
        );
    }

    public function store(Request $request)
    {
        $attachment = $request->get('attachment');
        $this->validate($request, [
            'attachment' => ['required', 'exists:attachments,id'],
            'sort_id' => 'integer|min:1',
            'link' => 'url'
        ], [
            'attachment.required' => '请上传轮播图',
            'attachment.exists' => '上传的轮播图不存在',
            'sort_id.integer' => '排序号不合法',
            'sort_id.min' => '排序号不得小于1',
            'link.url' => '不合法的跳转链接'
        ]);
        if (Banner::query()->count() >= 5) {
            $this->response->errorBadRequest('轮播图最多五张');
        }

        try {
            \DB::beginTransaction();

            $banner = Banner::query()->create([
                'sort_id' => $request->get('sort_id', 1),
                'link' => $request->get('link', '')
            ]);
            $banner->attachments()->sync($attachment);

            \App\Services\Banner::freshBannerListCache(); // 刷新缓存

            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            $this->response->errorBadRequest($e->getMessage());
        }

        return $this->response->created(null, $banner);
    }

    public function update(Request $request, $id)
    {
        $attachment = $request->get('attachment');
        $this->validate($request, [
            'attachment' => ['required', 'exists:attachments,id'],
            'sort_id' => 'integer|min:1',
            'link' => 'url'
        ], [
            'attachment.required' => '请上传轮播图',
            'attachment.exists' => '上传的轮播图不存在',
            'sort_id.integer' => '排序号不合法',
            'sort_id.min' => '排序号不得小于1',
            'link.url' => '不合法的跳转链接'
        ]);
        try {
            $banner = Banner::query()->findOrFail($id);

            \DB::beginTransaction();

            $banner->update($request->only(['sort_id', 'link']));
            $banner->attachments()->sync($attachment);

            \App\Services\Banner::freshBannerListCache(); // 刷新缓存

            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            $this->response->errorBadRequest($e->getMessage());
        }

        return $this->response->created(null, $banner);
    }

    public function delete($id)
    {
        try {
            \DB::beginTransaction();

            Banner::query()->find($id)->delete();
            \App\Services\Banner::freshBannerListCache();// 刷新缓存

            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            $this->response->errorBadRequest($e->getMessage());
        }

        return $this->response->noContent();
    }
}
